package C059.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AdjacencyList
 * Package: C059.graph
 * Description: 邻接表
 *
 * @Author BCXJ
 * @Create 2025/9/25 16:06
 * @Version 1.0
 * @Since 1.0
 */
public class AdjacencyList implements GraphInterface{
    List<List<int[]>> graph = new ArrayList<>();
    int size = 0;
    @Override
    public void init(int n, int m) {

    }

    @Override
    public void init(int n) {
        size = n;
        for (int i = 0; i < n + 1; i++) {
            graph.add(new ArrayList<int[]>());
        }
    }

    @Override
    public void addEdge(int from, int to, int weight) {
        int[] edge = {to, weight};
        graph.get(from).add(edge);
    }

    /**
     *     1(邻居、边权) : (5,5) (4,1)
     *     2(邻居、边权) : (4,6) (3,7) (5,4)
     *     3(邻居、边权) : (2,7) (4,2) (5,4)
     *     4(邻居、边权) : (2,6) (3,2) (1,1)
     *     5(邻居、边权) : (1,5) (2,4) (3,4)
     */
    @Override
    public void print() {
        System.out.println("=============邻接表标准输出===============");
        for (int i = 1; i <= size; i++) {
            System.out.print(i + "(邻边、边权)：");
            for (int[] edge : graph.get(i)) {
                System.out.print(String.format("(%s, %s) ", edge[0], edge[1]));
            }
            System.out.println();
        }
    }
}
